package com.whh.downloader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class CN2EN {
	private static Map<String,Map<String,String>> res;
	
	public static void main(String[] args) {

	}
	
	public static Map<String,Map<String,String>> getCN2ENMap(){
		if(res!=null){
			return res;
		}
		res = new HashMap<String, Map<String,String>>();
		try{
			String path = new CN2EN().getClass().getResource("/").getPath();
			File file = new File(path+"CN2EN");
			BufferedReader br = new BufferedReader(new FileReader(file));
			String str = br.readLine();
			while(str!=null){
				String[] objs = str.split(",");
				String nameCn = objs[0];
				String headerVert = objs[1];
				String roles = objs[2];
				String abilityStr = "";
				for(int i=3;i<objs.length;i++){
					abilityStr=abilityStr+objs[i];
					if(i!=objs.length-1){
						abilityStr=abilityStr+",";
					}
				}
				Map<String,String> map = new HashMap<String, String>();
				map.put("headerVert", headerVert);
				map.put("roles", roles);
				map.put("abilityStr", abilityStr);
				res.put(nameCn, map);
				str = br.readLine();
			}
			br.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public static void exportCN2EN(){
		try{
			List<String> nameCns = exportTgbugHerosName();
			Document doc = Jsoup.connect("http://www.dota2.com/heroes/").get();
			Element heroPickerInner = doc.getElementById("heroPickerInner");
			Elements as = heroPickerInner.select("a:has(img)");
			for(int i=0;i<nameCns.size();i++){
				String str=nameCns.get(i)+",";
				Document heroDoc = Jsoup.connect(as.get(i).attr("href")).get();
				String headerVert = heroDoc.getElementById("heroPrimaryPortraitHolder").select("img").first().attr("src");
				headerVert=headerVert.substring(headerVert.lastIndexOf("/")+1,headerVert.lastIndexOf("."));
				str=str+headerVert+",";
				String roles = heroDoc.getElementById("heroBioRoles").text().trim();
				roles = roles.substring(roles.indexOf("-")+2, roles.length());
				str=str+roles+",";
				Elements abilityImgs = heroDoc.getElementById("overviewHeroAbilities").select("img[class=overviewAbilityImg]");
				for(int j=0;j<abilityImgs.size();j++){
					String imgName = abilityImgs.get(j).attr("src");
					imgName=imgName.substring(imgName.lastIndexOf("/")+1,imgName.lastIndexOf("."));
					str=str+imgName;
					if(j!=abilityImgs.size()){
						str=str+",";
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static List<String> exportTgbugHerosName(){
		List<String> list = new ArrayList<String>();
		String url = "http://dota2.tgbus.com/heroes/";
		try{
			Document doc = Jsoup.connect(url).get();
			Elements llths = doc.select("div[class=left heroes] div[class=herotavern]").get(0).select("li a img");
			System.out.println("--力量天辉");
			for(int i=0;i<llths.size();i++){
				list.add(llths.get(i).attr("title"));
				System.out.println(llths.get(i).attr("title"));
			}
			Elements mjths = doc.select("div[class=left heroes] div[class=herotavern]").get(2).select("li a img");
			System.out.println("--敏捷天辉");
			for(int i=0;i<mjths.size();i++){
				list.add(mjths.get(i).attr("title"));
				System.out.println(mjths.get(i).attr("title"));
			}
			Elements zlths = doc.select("div[class=left heroes] div[class=herotavern]").get(4).select("li a img");
			System.out.println("--智力天辉");
			for(int i=0;i<zlths.size();i++){
				list.add(zlths.get(i).attr("title"));
				System.out.println(zlths.get(i).attr("title"));
			}
			
			Elements llyys = doc.select("div[class=left heroes] div[class=herotavern]").get(1).select("li a img");
			System.out.println("--力量夜魇");
			for(int i=0;i<llyys.size();i++){
				list.add(llyys.get(i).attr("title"));
				System.out.println(llyys.get(i).attr("title"));
			}
			Elements mjyys = doc.select("div[class=left heroes] div[class=herotavern]").get(3).select("li a img");
			System.out.println("--敏捷夜魇");
			for(int i=0;i<mjyys.size();i++){
				list.add(mjyys.get(i).attr("title"));
				System.out.println(mjyys.get(i).attr("title"));
			}
			Elements zlyys = doc.select("div[class=left heroes] div[class=herotavern]").get(5).select("li a img");
			System.out.println("--智力夜魇");
			for(int i=0;i<zlyys.size();i++){
				list.add(zlyys.get(i).attr("title"));
				System.out.println(zlyys.get(i).attr("title"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}
}
